package com.sg.service.biz.dataassets.nb;

import cn.hutool.core.bean.BeanUtil;
import com.sg.dto.biz.dataassets.req.DeletePhysicalResourceFileReqDto;
import com.sg.dto.biz.dataassets.req.ImplementUnstructuredDataReceiveFieldReqDto;
import com.sg.dto.biz.dataassets.res.DeletePhysicalResourceFileRespDto;
import com.sg.dto.biz.dataassets.res.ImplementUnstructuredDataReceiveFieldRespDto;
import com.wicket.okrcomponent.common.exception.BizException;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import java.io.File;

/**
 * @author ： 手工接入方法
 * @version 1.0
 * @since 2022/5/28 15:32
 */
@Service
public class NbResourceFile {
    /**
     * code:deleteResourcePhyFile
     * name:M8删除物理资源文件（特殊方法）
     * desc:undefined
     * gen by moon at 11/11/2024, 1:31:02 AM
     **/
    @Trace(operationName = "M8删除物理资源文件（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public DeletePhysicalResourceFileRespDto deletePhysicalResourceFile(DeletePhysicalResourceFileReqDto reqDto) {
        // TODO ruizhe skai dong ; 1/25/2024, 9:46:36 PM
        String rootPath = reqDto.getFileStorageRootPath();
        String relPath = reqDto.getResourceFileRelaPath();
        if (reqDto.getFileStorageRootPath() != null && reqDto.getFileStorageRootPath().endsWith("/")) {
            rootPath = rootPath.substring(0, rootPath.length() - 1);
        }

        if (reqDto.getResourceFileRelaPath() != null && !reqDto.getResourceFileRelaPath().startsWith("/")) {
            relPath = "/" + relPath;
        }

        if (rootPath == null || rootPath.isEmpty() || relPath == null || relPath.isEmpty()) {
            throw new BizException("-1", "文件根路径或者文件相对路径为空", false);
        }
        String targetFilePath = rootPath + relPath;
        excuteDelFile(targetFilePath);
        return new DeletePhysicalResourceFileRespDto();
    }

    @Trace(operationName = "执行磁盘删除")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public void excuteDelFile(String filePath) {


        // 创建一个File对象
        File file = new File(filePath);
        if (!file.exists()) {
            return;
        }

        // 调用delete方法尝试删除文件
        boolean result = file.delete();

        // 检查文件是否成功被删除
        if (result) {

        } else {
            throw new BizException("-1", "文件删除失败", false);
        }
    }

    /**
     * code:receptionService
     * name:M8执行非结构化数据接收入参字段入参
     * desc:undefined
     * gen by moon at 12/8/2024, 3:50:31 AM
     **/
    @Trace(operationName = "M8执行非结构化数据接收入参字段入参")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementUnstructuredDataReceiveFieldRespDto implementUnstructuredDataReceiveField(ImplementUnstructuredDataReceiveFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementUnstructuredDataReceiveFieldRespDto.class);
    }
    //手工接入方法
}
